﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：UserAdd.cs
    // 文件功能描述：主用户，主用户注册
    // 创建标识：吕海乐2009-03-10
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Text.RegularExpressions;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class UserAdd : BasePage
    {

        IBLL_Hospital ih = Factory.BLL_Hospital();
        IBLL_Company ic = Factory.BLL_Company();

        Model.HospitalInfo hospital = new HospitalInfo();
        Model.CompanyInfo companyinfo = new CompanyInfo();
        HZ.DrugTrade.BLL.BLL_Log log = new BLL.BLL_Log();


        public UserAdd()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.主用户管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            { 
                string id = Request.QueryString["roleID"];
                string hid = "";
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    hid = Request["id"];
                }


                /*
                 <option value="01">医疗机构</option>
	<option value="02">配送企业</option>
	<option value="03">管理机构</option>
	<option value="05">生产企业</option>
                 
                 */
                if (id.Equals("01"))
                {
                    hospital = ih.GetHospitalInfoByID(hid);

                    txtUserId.Text = hospital.HospitalID;
                    txtUserName.Text = hospital.HospitalName;
                    txtUserId.Enabled = false;
                    txtUserName.Enabled = false;
                }
                if (id.Equals("02") || id.Equals("05"))
                {

                    companyinfo = ic.GetCompanyInfoByID(hid);

                    if (companyinfo==null)
                    {
                        HZ.Web.WebUtil.Alert("企业信息不全！",this);
                        return;
                    }
                    txtUserId.Text = companyinfo.CompanyID;
                    txtUserName.Text = companyinfo.CompanyName;
                    txtUserId.Enabled = false;
                    txtUserName.Enabled = false;
                }
                

                //初始化用户密码
                txtUserPwd.Text =Constants.USER_DEFAULT_PWS;
                
            }

        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HZ.DrugTrade.Model.User user = new User();
                //20100119 注释
                ////检查用户ID规则
                //if (RegexValid(txtUserId.Text.Trim(), "^[SJPZsjpz]{1}[0-9]{4}$"))
                //{
                //    HZ.Web.WebUtil.Alert("用户ID不符合规则，不能类似X0001！", this);
                //    return;
                //}


                //获取用户基本信息
                user.UserID = this.txtUserId.Text.Trim();
                user.UserName = this.txtUserName.Text.Trim();
                user.UserPassword = this.txtUserPwd.Text.Trim();
                user.IsOnline = false;
                user.IsUserAdmin = true;

                if (rblIsUsing.SelectedValue == "0")
                {
                    user.IsUsing = false;
                }
                else
                {
                    user.IsUsing = true;
                }

                if (rblIsAdmin.SelectedValue == "0")
                {
                    user.IsAdmin = false;
                }
                else
                {
                    user.IsAdmin = true;
                }

                Role role = new Role();
                role.RoleID = Request["roleId"];
                Org org = new Org();
                org.OrgID = Request["id"];
                user.UserRole = role;
                user.Org = org;
                //获取用户的操作权限
                List<OperatingRule> operatingRuleList = new List<OperatingRule>();
                string ids = Request["checkbox1"];
                if (!string.IsNullOrEmpty(ids))
                {
                    string[] arraryIds = ids.Split(',');
                    foreach (string Item in arraryIds)
                    {
                        OperatingRule otr = new OperatingRule();
                        otr.OperatingRuleID = Item;
                        operatingRuleList.Add(otr);
                    }
                }

                //调用AddUser_OperatingRule方法
                IBLL_User iu = Factory.BLL_User();
                string flag = iu.AddUser_OperatingRule(user, operatingRuleList).ToString();

                //判断操作情况
                switch (flag)
                {
                    case "0":
                        HZ.Web.WebUtil.Alert("添加失败！",this);
                        break;
                    case "1":
                        log.AddKeyOperationLog(new KeyOperationLog() { OperateMessage = string.Format("注册新的用户{0}", txtUserId.Text) });
                        HZ.Web.MessageBox.AlertAndRedirect("添加成功！", "UserList.aspx");
                        break;
                    case "2":
                        HZ.Web.WebUtil.Alert("用户编号已存在！",this);
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 匹配正则表单式
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private bool RegexValid(string input, string pattern)
        {
            Regex regex = new Regex(pattern);
            if (input != null)
            {
                return regex.IsMatch(input);
            }
            else
            {
                return false;
            }
        }
    }
}
